/**
 * 
 */
package com.williamoneil.charts.client;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.williamoneil.charts.client.controller.ChartController;
import com.williamoneil.charts.client.data.ChartType;
import com.williamoneil.charts.shared.PeriodicityType;
import com.williamoneil.charts.shared.SymbolData;

/**
 * @author Gudipati
 *
 */
public class ChartHeaderPanel extends DockLayoutPanel {

	private Label companyName = null;
	private TextBox symbolText = null;
	
	private String previousSymbol = null;
	
	private PeriodicityType periodicity = PeriodicityType.Weekly; 
	
	ListBox periodicityListBox = null;
	ListBox scaleListBox = null;
	ListBox themeListBox = null;
	ListBox chartTypeListBox = null;
	ListBox tickSpacingListBox = null;
	
	public ChartHeaderPanel() {
		super(Unit.EM);
		
		
		// Create a Horizontal Panel
	    HorizontalPanel hPanel = new HorizontalPanel();
	    hPanel.setSpacing(5);
	    
	    companyName = new Label("");
	    hPanel.add(companyName);
	    
	    
	    periodicityListBox = new ListBox();
	    hPanel.add(periodicityListBox);
	    
	    periodicityListBox.addItem(PeriodicityType.Daily.name());
	    periodicityListBox.addItem(PeriodicityType.Weekly.name());
	    periodicityListBox.addItem(PeriodicityType.Monthly.name());
	    periodicityListBox.setItemSelected(1, true);
	    
	    themeListBox = new ListBox();
	    hPanel.add(themeListBox);
	    
	    themeListBox.addItem("Light");
	    themeListBox.addItem("Dark");
	    themeListBox.setItemSelected(0, true);
	    
	    scaleListBox = new ListBox();
	    hPanel.add(scaleListBox);
	    scaleListBox.addItem("Log");
	    scaleListBox.addItem("Linear");
	    scaleListBox.setItemSelected(0, true);
	    
	    chartTypeListBox = new ListBox();
	    hPanel.add(chartTypeListBox);
	    chartTypeListBox.addItem(ChartType.CANDLESTICK.name());
	    chartTypeListBox.addItem(ChartType.HLOC.name());
	    chartTypeListBox.addItem(ChartType.HLC.name());
	    chartTypeListBox.setItemSelected(0, true);
	    
	    tickSpacingListBox = new ListBox();
	    hPanel.add(tickSpacingListBox);
	    tickSpacingListBox.addItem("4");
	    tickSpacingListBox.addItem("5");
	    tickSpacingListBox.addItem("6");
	    tickSpacingListBox.addItem("7");
	    
	    periodicityListBox.addChangeHandler(new ChangeHandler() {
	      public void onChange(ChangeEvent event) {
	        updatePeriodicity();
	      }
	    });
	    themeListBox.addChangeHandler(new ChangeHandler() {
		      public void onChange(ChangeEvent event) {
		        updateTheme();
		      }
		    });
	    scaleListBox.addChangeHandler(new ChangeHandler() {
		      public void onChange(ChangeEvent event) {
		        updateScale();
		      }
		    });
	    chartTypeListBox.addChangeHandler(new ChangeHandler() {
		      public void onChange(ChangeEvent event) {
		        updateChartType();
		      }
		    });
	    tickSpacingListBox.addChangeHandler(new ChangeHandler() {
		      public void onChange(ChangeEvent event) {
		        updateTickSpacing();
		      }
		    });
	    
		super.addWest(hPanel, 100);
		
		symbolText = new TextBox();
		super.addEast(symbolText, 9);
		
		super.setTitle("Chart Header");
		
		symbolText.setMaxLength(8);
		//symbolText.setVisibleLength(6);
		symbolText.setEnabled(true);
		symbolText.setValue("");
		symbolText.setFocus(true);
		
		symbolText.addKeyUpHandler(new KeyUpHandler() {
			@Override
			public void onKeyUp(KeyUpEvent event) {
				final int keyCode = event.getNativeKeyCode();
				if(Charts.getSubmitKeyCodes().contains(keyCode)) {
					performChartLoad(symbolText.getText());
				} else if (keyCode == KeyCodes.KEY_ESCAPE) {
					symbolText.setText(previousSymbol == null ? "" : previousSymbol);
				}
			}
		});
	}
	
	private void performChartLoad(String symbolStr) {
		try {
			if(symbolStr != null && symbolStr.length() > 0) {
				final String symbol = symbolStr.trim().toUpperCase();
				
				ChartController.getInstance().loadSymbol(symbol, periodicity);
			}
			previousSymbol = symbolText.getText();
		}catch(Exception ex) {
			Charts.showError(ex);
		}
		
		final String symbolTextStr = symbolText.getText();
		if(symbolTextStr != null && symbolTextStr.length() > 0) {
			symbolText.setSelectionRange(0, symbolTextStr.length());
		}
	}
	
	public void setSymbolInformation(final SymbolData symbolInfo) {
		if(symbolInfo != null) {
			symbolText.setText(symbolInfo.getSymbol());
			companyName.setText(symbolInfo.getName());
		} else {
			companyName.setText(symbolText.getText());
		}
		
		previousSymbol = symbolText.getText();
	}
	
	public void updatePeriodicity() {
		final String periodicityStr = periodicityListBox.getValue(periodicityListBox.getSelectedIndex());
		final PeriodicityType selectedPeriodicity = PeriodicityType.valueOf(periodicityStr);
		if(selectedPeriodicity != null && selectedPeriodicity != this.periodicity) {
			this.periodicity = selectedPeriodicity;
		}
	
		if(previousSymbol != null) {
			performChartLoad(previousSymbol);
		}
	}
	
	public void updateTheme() {
		final String themeStr = themeListBox.getValue(themeListBox.getSelectedIndex());
		
		boolean lightTheme = false;
		if(themeStr != null && themeStr.equalsIgnoreCase("Light")) {
			lightTheme = true;
		}
		
		Charts.getChartMainPanel().getChart().changeTheme(lightTheme);
	}
	
	public void updateScale() {
		final String scaleStr = scaleListBox.getValue(scaleListBox.getSelectedIndex());
		
		boolean isLinear = false;
		if(scaleStr != null && scaleStr.equalsIgnoreCase("Linear")) {
			isLinear = true;
		}
		
		Charts.getChartMainPanel().getChart().changeScale(isLinear);
	}
	
	public void updateChartType() {
		final String typeStr = chartTypeListBox.getValue(chartTypeListBox.getSelectedIndex());
		final ChartType chartType = ChartType.valueOf(typeStr);
		
		if(chartType != null) {
			Charts.getChartMainPanel().getChart().changeChartType(chartType);
		}
	}
	
	public void updateTickSpacing() {
		final String valueStr = tickSpacingListBox.getValue(tickSpacingListBox.getSelectedIndex());
		final int tickSpacing = Integer.parseInt(valueStr);
		
		if(tickSpacing > 0) {
			Charts.getChartMainPanel().getChart().changeTickSpacing(tickSpacing);
		}
	}
}
